﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Write.API.Application.Commands.HospitalNavigationCommand;
using MediatR;

namespace D.Medicalcare.Write.API.Application.Handlers.HospitalNavigationHandler
{
    /// <summary>
    /// 修改医院导航信息处理器
    /// </summary>
    public class UpdateHospitalNavigationHandler : IRequestHandler<UpdateHospitalNavigationCommand, APIResult<int>>
    {
        private readonly IBaseRepository<HospitalNavigation> baseRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseRepository"></param>
        public UpdateHospitalNavigationHandler(IBaseRepository<HospitalNavigation> baseRepository)
        {
            this.baseRepository = baseRepository;
        }
        /// <summary>
        /// 修改医院导航信息处理器方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> Handle(UpdateHospitalNavigationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> result = new APIResult<int>();

                var model = baseRepository.GetAll().FirstOrDefault(x => x.HospitalNavigationId == request.HospitalNavigationId);
                model.CampusName = request.CampusName;
                model.ContactNumber = request.ContactNumber;
                model.PublicTransit = request.PublicTransit;
                model.AddressCampus = request.AddressCampus;
                model.CampusStatus = request.CampusStatus;
                model.CampusStatus = request.CampusStatus;

                var dto = baseRepository.Update(model).Result;

                if (dto > 0)
                {
                    result.Code = APIEnum.Success;
                    result.Msg = "修改成功";
                    result.Data = dto;
                }
                else
                {
                    result.Code = APIEnum.Failed;
                    result.Msg = "修改成功";
                    return await Task.FromResult(result);
                }
                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
